package com.billdowney.qq.robot.util;

import org.apache.commons.lang3.StringUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {
    private MD5Util() {
    }

    /**
     * MD5加密
     *
     * @param strSrc 原文
     * @param salt   盐
     * @return 加密后的字符串
     */
    public static String cryptMD5(String strSrc, String salt) {
        if (StringUtils.isEmpty(strSrc)) {
            throw new IllegalArgumentException("param strSrc cannot be null or zero length");
        }
        if (StringUtils.isEmpty(salt)) {
            throw new IllegalArgumentException("param salt cannot be null or zero length");
        }
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(strSrc.getBytes());

            StringBuilder result = new StringBuilder();
            byte[] temp = md5.digest(salt.getBytes());
            for (byte b : temp) {
                result.append(Integer.toHexString((0x000000ff & b) | 0xffffff00).substring(6));
            }

            return result.toString();
        } catch (NoSuchAlgorithmException e) {
        }
        return null;
    }

    /**
     * MD5加密
     *
     * @param strSrc 加密前的字符串
     * @return 加密后的字符串
     */
    public static String cryptMD5(String strSrc) {
        if (StringUtils.isEmpty(strSrc)) {
            throw new IllegalArgumentException("param strSrc cannot be null or zero length");
        }
        StringBuilder hexString = new StringBuilder();

        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(strSrc.getBytes());
            byte[] hash = md.digest();
            for (byte b : hash) {
                if ((0xff & b) < 0x10) {
                    hexString.append("0");
                    hexString.append(Integer.toHexString((0xFF & b)));
                } else {
                    hexString.append(Integer.toHexString(0xFF & b));
                }
            }

        } catch (NoSuchAlgorithmException e) {
        }
        return hexString.toString();
    }

    /**
     * Md5加密
     *
     * @param strSrc 明文
     * @param salt   盐
     * @param count  散列次数
     * @return 密文
     */
//    public static String cryptMD5(String strSrc, String salt, int count) {
//        return new Md5Hash(strSrc, salt, count < 1 ? 1 : count).toString();
//    }
    
    public static void main(String []arg){
        System.out.println(cryptMD5("111111", "1234"));
    }
        

}
